<style>
    .layui-form-label {
        width: 100px;
    }
    .layui-input-block {
        margin-left: 130px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">报修时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input" type="text"
                                               placeholder="选择时间" autocomplete="off" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startTime" id="startTime" type="hidden"/>
                                    <input name="endTime" id="endTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">盘验编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入盘验编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">柜子编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="cabCode" class="layui-input" type="text" placeholder="请输入柜子编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">部门：</label>
                                    <div class="layui-input-inline">
                                        <select name="departmentId" lay-filter="departmentId" lay-search="" class="common-select-search"></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">操作人：</label>
                                    <div class="layui-input-inline">
                                        <select name="creatorId" lay-search="" class="common-select-search"></select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="storageInventoryInoutBtnGenOrder" class="layui-btn icon-btn color-reseda">合并生成报修订单</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="storageInventoryInoutBtnExport" class="layui-btn icon-btn color-green">导出
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="storageCheckRepairResultTable" lay-filter="storageCheckRepairResultTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="storageCheckRepairResultTableTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    {{# if(!d.isGenOrder){ }}
    <a class="layui-btn layui-btn-xs" lay-event="genOrder">报修订单</a>
    {{#} }}
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
</script>

<script type="text/html" id="storageCheckRepairResultTable4GenOrderTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
</script>

<script type="text/html" id="storageInventoryInoutTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>

    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="storageInventoryInoutForm">
    <form lay-filter="storageInventoryInoutForm" class="layui-form model-form form-scroll">
        <div class="open-details-box pd-20 is-detail">
            <input name="id" type="hidden"/>
            <div class="color-title" style="margin: 15px 0;">盘验任务信息</div>
            <div class="layui-form-item">
                <div class="layui-col-md4">
                    <label class="layui-form-label">柜子名称</label>
                    <div class="layui-input-block">
                        <input name="cabName" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">柜子编号</label>
                    <div class="layui-input-block">
                        <input name="cabCode" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">所属货位</label>
                    <div class="layui-input-block">
                        <input name="spaceCode" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-col-md4">
                    <label class="layui-form-label">盘验任务编号</label>
                    <div class="layui-input-block">
                        <input name="code" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">盘验时间</label>
                    <div class="layui-input-block">
                        <input name="createTime" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">盘验员</label>
                    <div class="layui-input-block">
                        <input name="creatorName" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-col-md4">
                    <label class="layui-form-label">盘验前状态</label>
                    <div class="layui-input-block">
                        <input name="businessTypeDesc" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">盘验结果状态</label>
                    <div class="layui-input-block">
                        <input name="businessTypeCheckedDesc" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="color-title" style="margin: 15px 0;">设备信息</div>
            <div class="layui-form-item">
                <div class="layui-col-md4">
                    <label class="layui-form-label">所属企业</label>
                    <div class="layui-input-block">
                        <input name="enterpriseName" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">所属车间</label>
                    <div class="layui-input-block">
                        <input name="workshopName" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">车间描述</label>
                    <div class="layui-input-block">
                        <input name="workshopDesc" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
            </div>
            <div class="color-title" style="margin: 15px 0;">物料信息</div>
            <table class="layui-table" id="storageInventoryInoutDetailTable" lay-filter="storageInventoryInoutDetailTable"></table>
        </div>
        <div class="layui-form-item text-right pd-20">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script type="text/html" id="detailTable">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar" id="toolbar2">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">柜子编号：</label>
                            <div class="layui-input-inline mr0">
                                <input name="cabCode" class="layui-input" type="text" placeholder="请输入柜子编号"/>
                            </div>
                            <label class="layui-form-label w-auto">物料编号：</label>
                            <div class="layui-input-inline mr0">
                                <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                            <!--
                            <button id="storageInventoryInoutBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            -->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="storageInventoryInoutTable" lay-filter="storageInventoryInoutTable"></table>
            </div>
        </div>
    </div>
    <form id="genOrderForm2" lay-filter="genOrderForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择供应商</label>
            <div class="layui-input-block">
                <select id="supplierId2" name="supplierId" lay-filter="supplierId" lay-search lay-verify="required">
                    <option value=""> 请选择供应商</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">物流联系人</label>
            <div class="layui-input-block">
                <input name="logisticsContact" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货联系人</label>
            <div class="layui-input-block">
                <input name="receiveContact" placeholder="" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货人电话</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货人地址</label>
            <div class="layui-input-block">
                <input name="addr" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">报修费用</label>
            <div class="layui-input-block">
                <input name="price" placeholder="" type="text" class="layui-input" lay-verify="required|number" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物流单号</label>
            <div class="layui-input-block">
                <input name="expressNo" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>


        <div align="center" >
            <div class="layui-form-item text-right saveCancelBtn" >
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="storageInventoryInoutBtnGenOrderSubmit2" lay-submit>提交</button>
            </div>
        </div>
        <br>
    </form>
</script>


<script type="text/html" id="genOrderTable">

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <!--
                <table class="layui-table" id="storageCheckRepairResultTable4GenOrderInfo" lay-filter="storageCheckRepairResultTable4GenOrderInfo"></table>
                <br>
                -->
                <!-- 表格 -->
                <table class="layui-table" id="storageCheckRepairResultTable4GenOrder" lay-filter="storageCheckRepairResultTable4GenOrder"></table>


            </div>
        </div>
    </div>
    <form lay-filter="genOrderForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择供应商</label>
            <div class="layui-input-block">
                <select  name="supplierId" lay-filter="supplierId" lay-search lay-verify="required">
                    <option value=""> 请选择供应商</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">物流联系人</label>
            <div class="layui-input-block">
                <input name="logisticsContact" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货联系人</label>
            <div class="layui-input-block">
                <input name="receiveContact" placeholder="" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货人电话</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收货人地址</label>
            <div class="layui-input-block">
                <input name="addr" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">报修费用</label>
            <div class="layui-input-block">
                <input name="price" placeholder="" type="text" class="layui-input" lay-verify="required|number" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物流单号</label>
            <div class="layui-input-block">
                <input name="expressNo" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>


        <div align="center" >
            <div class="layui-form-item text-right saveCancelBtn" >
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="storageInventoryInoutBtnGenOrderSubmit" lay-submit>提交</button>
            </div>
        </div>
        <br>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        admin.selectSearch();

        var currentPageData=[];
        // 渲染表格
        var insTb = table.render({
            elem: '#storageCheckRepairResultTable',
            url: config.base + 'enterprisecab/cabinet/admin/checkedRepairResult/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'skuName', title: '物料名称', fixed: 'left', width: 160}
                ,{field: 'skuCode', title: '物料编号', fixed: 'left', width: 160}
                ,{field: 'skuSpecOld', title: '规格码', fixed: 'left', width: 160}
                ,{field: 'productBrandName', title: '品牌'}
                ,{field: 'code', sort: true, title: '盘验编号', width: 160}
                , {field: 'departmentName', sort: false, title: '部门'}
                , {field: 'costCenter', sort: false, title: '成本中心'}
                ,{field: 'cabCode', sort: true, title: '柜子编号', width: 130}
                ,{field: 'cabName', title: '柜子名称'}
                ,{field: 'spaceCode', sort: true, title: '货位编号'}
                ,{field: 'quantity', sort: true, title: '数量'}
                // ,{field: 'inType', sort: true, title: '存入方式',templet:function (d) {
                //         return '报废';
                //     }}
                ,{field: 'businessTypePreDesc', title: '盘验前状态', width: 120}
                ,{field: 'businessTypeDesc', title: '盘验后状态', width: 120}
                // ,{field: 'businessType', sort: true, title: '存储性质',templet:function (d) {
                //         if(d.businessType=='temp'){
                //             return '暂存';
                //         }else if(d.businessType=='repair'){
                //             return '报修'
                //         }else if(d.businessType=='recycle'){
                //             return '报废'
                //         }
                //     }}
                ,{field: 'creatorName', sort: true, title: '盘验人', width: 110}
                ,{field: 'createTime', sort: true, title: '盘验时间', width: 160}
                // ,{field: 'isCheck', sort: true, title: '是否盘验',templet:function (d) {
                //         if(d.status=='checked'){
                //             return '是';
                //         }
                //         return  '否';
                //     }}
                ,{field: 'isGenOrder', sort: true, title: '是否已生成订单', width: 140,templet:function (d) {
                        if(d.isGenOrder){
                            return '是'
                        }
                        return '否';
                    }}
                // ,{field: 'no', sort: true, title: 'no'}
                // ,{field: 'day', sort: true, title: 'day'}
                // ,{field: 'operationTypeDesc', sort: true, title: '操作类型'}
                // ,{field: 'businessTypeDesc', sort: true, title: '业务类型'}
                // ,{field: 'statusDesc', sort: true, title: '状态'}
                // ,{field: 'checkTime', sort: true, title: '盘验时间'}
                // ,{field: 'checkUserId', sort: true, title: '盘验人id'}
                // ,{field: 'checkUserName', sort: true, title: '盘验人'}
                // ,{field: 'notifyTime', sort: true, title: '通知时间'}
                // ,{field: 'notifyUserId', sort: true, title: '通知人'}
                // ,{field: 'notifyUserName', sort: true, title: '通知人'}
                // ,{field: 'takeUserId', sort: true, title: '回收人id'}
                // ,{field: 'takeUserName', sort: true, title: '回收人'}
                // ,{field: 'takeTime', sort: true, title: '回收时间'}
                // ,{field: 'inoutId', sort: true, title: '暂存柜存刀id'}
                // ,{field: 'cabId', sort: true, title: '柜子id'}
                // ,{field: 'skuId', sort: true, title: 'sku id'}
                // ,{field: 'companyId', sort: true, title: '企业id'}
                // ,{field: 'spaceId', sort: true, title: '货道id'}
                // ,{field: 'operationTypeDesc', sort: true, title: '操作类型'}
                // ,{field: 'businessTypeDesc', sort: true, title: '业务类型'}
                // ,{field: 'quantityCurrent', sort: true, title: '当前库存'}
                // ,{field: 'quantityAfter', sort: true, title: '变动后库存'}

                ,{align: 'center', toolbar: '#storageCheckRepairResultTableTableBar', title: '操作', width: 160, fixed: 'right'}
            ]],
            done: function (res, curr, count) {
                currentPageData=res.data;
                var isCheckAll=true;
                var isAllGenOrder=true;
                for(var k=0;k<res.data.length;k++){
                    res.data[k].LAY_CHECKED = true;
                    var index= res.data[k]['LAY_TABLE_INDEX'];
                    var checkbox = $('#'+'storageCheckRepairResultTable'+'').next().find('tr[data-index=' + index + '] input[type="checkbox"]');
                    if(checkedMap.get(res.data[k].id)){
                        checkbox.prop('checked', true).next().addClass('layui-form-checked');
                    }else{
                        isCheckAll=false;
                    }
                    if(res.data[k].isGenOrder){
                        $(checkbox[0]).parent().remove();
                    }else{
                        isAllGenOrder=false;
                    }
                }
                if(isCheckAll){
                    $('[lay-filter="layTableAllChoose"]').prop('checked', true).next().addClass('layui-form-checked');
                }
                if(isAllGenOrder){
                    $($('[lay-filter="layTableAllChoose"]')[0]).parent().hide();
                }else{
                    $($('[lay-filter="layTableAllChoose"]')[0]).parent().show();
                }
                //移除按钮

                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(storageCheckRepairResultTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='detail'){
                data.eventType='detail'
                showDetailModel(data);
            }else if(obj.event=='genOrder'){
                data.eventType='genOrder'
                showDetailModel(data);
            }

        });

        // 部门
        admin.req('user/admin/department/list', {limit:9999}, res => {
            if (res.code == 200){
                loadReportSelectHtml(res.data,'请选择部门','departmentId',1,'.lay-header')
            }else {
                loadReportSelectHtml([],'请选择部门','departmentId',1,'.lay-header')
            }
        });

        //部门操作
        form.on('select(departmentId)',function(data){
            selectManager(data.value);
        });

        // 操作人
        function selectManager(_data) {
            if (_data){
                admin.req('user/admin/user/list', {limit:9999, departmentId:_data}, res => {
                    if (res.code == 200){
                        loadReportSelectHtml(res.data,'请选操作人','creatorId',2,'.lay-header')
                    }else {
                        loadReportSelectHtml([],'请选操作人','creatorId',2,'.lay-header')
                    }
                });
            }else {
                loadReportSelectHtml([],'请选操作人','creatorId',2,'.lay-header')
            }
        }

        //监听排序
        table.on('sort(storageCheckRepairResultTable)', function(obj){
            table.reload('storageCheckRepairResultTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });
        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        var checkedMap=new Map();
        var checkedData=[];
        table.on('checkbox(storageCheckRepairResultTable)', function(obj){
            var data = obj.data;
            if(obj.type=='all'){
                if(obj.checked){
                    var checkStatus = table.checkStatus('storageCheckRepairResultTable')
                    var checkedAllData = checkStatus.data;
                    checkedAllData.forEach(item=> {
                        if(!item.isGenOrder){
                            if (checkedMap.get(item.id) == undefined) {
                                checkedData.push(item);
                            }
                            checkedMap.set(item.id,item.id);
                        }

                    })


                }else{
                    currentPageData.forEach(item=>{
                        if(checkedMap.get(item.id)!=undefined){
                            checkedData=checkedData.filter(item2=>{
                                if(item.id==item2.id){
                                    return false;
                                }
                                return  true;
                            })
                        }
                        checkedMap.delete(item.id);

                    })
                }
            }else{
                if(obj.checked){
                    if (checkedMap.get(data.id) == undefined) {
                        checkedData.push(data);
                    }
                    checkedMap.set(data.id,data.id);
                }else{
                    if(checkedMap.get(data.id)!=undefined){
                        checkedData=checkedData.filter(item=>{
                            if(item.id==data.id){
                                return false;
                            }
                            return  true;
                        })
                    }
                    checkedMap.delete(data.id);
                }
                var isAllChecked=true;
                currentPageData.forEach(item=>{
                    if(!item.isGenOrder){
                        if(checkedMap.get(item.id)==undefined){
                            isAllChecked=false;
                        }
                    }

                })
                if(isAllChecked){
                    $('[lay-filter="layTableAllChoose"]').prop('checked', true).next().addClass('layui-form-checked');
                }else{
                    $('[lay-filter="layTableAllChoose"]').prop('checked', false).next().removeClass('layui-form-checked');
                }
            }

        });

        // 添加按钮点击事件
        $('#storageInventoryInoutBtnAdd').click(function () {
            showEditModel();
        });

        $('#storageInventoryInoutBtnGenOrder').click(function () {
            if(checkedData.length==0){
                layer.msg('请选择报修记录', {icon: 2})
                return;
            }
            showEditGenOrderModel();
        });

        function showCheckedDataTable(data){
            var insTb = table.render({
                elem: '#storageCheckRepairResultTable4GenOrder',
                data: data,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'numbers'}

                    ,{field: 'code', sort: true, title: '盘验编号'}
                    // ,{field: 'inType', sort: true, title: '存入方式',templet:function (d) {
                    //         return '报废';
                    //     }}
                    ,{field: 'cabCode', sort: true, title: '柜子编号'}
                    ,{field: 'spaceCode', sort: true, title: '货位编号'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuCode', sort: true, title: '物料编号'}
                    ,{field: 'quantity', sort: true, title: '数量'}
                    ,{field: 'businessType', sort: true, title: '存储性质',templet:function (d) {
                            if(d.businessType=='temp'){
                                return '暂存';
                            }else if(d.businessType=='repair'){
                                return '报修'
                            }else if(d.businessType=='recycle'){
                                return '报废'
                            }
                        }}
                    ,{field: 'creatorName', sort: true, title: '盘验人'}
                    ,{field: 'createTime', sort: true, title: '盘验时间'}
                    // ,{field: 'isCheck', sort: true, title: '是否盘验',templet:function (d) {
                    //         if(d.status=='checked'){
                    //             return '是';
                    //         }
                    //         return  '否';
                    //     }}

                    // ,{field: 'status', sort: true, title: '盘验状态',templet:function (d) {
                    //         if(d.status=='created'){
                    //             return '待盘验'
                    //         }
                    //         return  '已盘验'
                    //     }}
                    // ,{field: 'isGenOrder', sort: true, title: '是否已生成订单',templet:function (d) {
                    //         if(d.isGenOrder){
                    //             return '是'
                    //         }
                    //         return '否';
                    //     }}
                    ,{align: 'center', toolbar: '#storageCheckRepairResultTable4GenOrderTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    // currentPageData=res.data;
                    //移除按钮

                    // var menuButton = config.getMenuButton(location.hash);
                    // if(menuButton.indexOf("搜索") == -1){
                    //     $('.layui-form.toolbar').remove();
                    // }
                    // if(menuButton.indexOf("修改") == -1){
                    //     $("[lay-event='edit']").remove();
                    // }
                    // if(menuButton.indexOf("删除") == -1){
                    //     $("[lay-event='del']").remove();
                    // }
                }
            });

            // 工具条点击事件
            table.on('tool(storageCheckRepairResultTable4GenOrder)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }else if(obj.event=='detail'){
                    data.eventType='detail'
                    showDetailModel(data);
                }
            });

            //监听排序
            table.on('sort(storageCheckRepairResultTable4GenOrder)', function(obj){
                table.reload('storageCheckRepairResultTable4GenOrder', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });
        }



        function showEditGenOrderModel(data) {

            admin.open({
                type: 1,
                area: '1480px',
                offset: '65px',
                title: '合并报修订单',
                content: $('#genOrderTable').html(),
                success: function () {
                    showCheckedDataTable(checkedData);

                    admin.req('enterprisecab/cabinet/app/supplier/list?page=1&limit=9999', {}, function (res) {
                        if (res.code == 200) {
                            var html='<option value="">请选择供应商</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-name="'+res.data[k].companyName+'" data-companyid="'+res.data[k].companyId+'">'+res.data[k].companyName+'</option>'
                            }
                            $("select[name='supplierId']").html(html);
                            form.render('select');
                        }
                    }, 'GET');

                    form.on('submit(storageInventoryInoutBtnGenOrderSubmit)', function (d) {
                        layer.load(2);
                        var detailList=[];
                        checkedData.forEach(item=>{
                            detailList.push({resultId:item.id,quantity:item.quantity})
                        })
                        d.field.detailList=detailList;
                        d.field.supplierName=$("select[name='supplierId'] option:selected").data('name');
                        d.field.supplierCompanyId=$("select[name='supplierId'] option:selected").data('companyid')
                        d.field.businessType='repair';
                        admin.req('enterprisecab/cabinet/admin/checkedResultOrder/save', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('storageCheckRepairResultTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                        checkedMap=new Map();
                        checkedData=[];
                        return false;
                    });



                }
            });
        }

        function showDetailModel(data){
            var title='';
            var pageBool=true;
            if(data.eventType=='detail'){
                title='详情';
            }else if(data.eventType=='genOrder'){
                title='报修订单生成';
                pageBool=false;

            }

            admin.open({
                type: 1,
                area: '1480px',
                offset: '65px',
                title: title,
                content: $('#detailTable').html(),
                success: function () {
                    if(data.eventType=='detail'){
                        $("#genOrderForm2").hide();
                    }else if(data.eventType=='genOrder'){
                        $("#toolbar2").hide();
                        admin.req('enterprisecab/cabinet/app/supplier/list?page=1&limit=9999', {}, function (res) {
                            if (res.code == 200) {
                                var html='<option value="">请选择供应商</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-name="'+res.data[k].companyName+'" data-companyid="'+res.data[k].companyId+'">'+res.data[k].companyName+'</option>'
                                }
                                $("#supplierId2").html(html);
                                form.render('select');
                            }
                        }, 'GET');

                    }
                    var insTb = table.render({
                        elem: '#storageInventoryInoutTable',
                        // url: config.base + 'enterprisecab/cabinet/admin/storageInventoryInout/list?checkResultId='+data.id,
                        url: config.base + 'enterprisecab/cabinet/admin/checkedResultDetail/list?resultId='+data.id,
                        page: pageBool,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers',title:"序号"}
                            ,{field: 'code', title: '盘验任务编号'}
                            // ,{field: 'inType', sort: true, title: '存入方式',templet:function (d) {
                            //         return '报废';
                            //     }}
                            ,{field: 'cabName', title: '柜子名称'}
                            ,{field: 'cabCode', sort: true, title: '柜子编号'}
                            ,{field: 'spaceCode', sort: true, title: '货位编号'}
                            ,{field: 'skuName', title: '物料名称'}
                            ,{field: 'skuCode', sort: true, title: '物料编号'}
                            ,{field: 'quantity', sort: true, title: '数量'}
                            ,{field: 'businessTypeDesc', title: '盘验前状态'}
                            ,{field: 'businessTypeCheckedDesc', title: '盘验后状态'}
                            // ,{field: 'businessType', sort: true, title: '存储性质',templet:function (d) {
                            //         if(d.businessType=='temp'){
                            //             return '暂存';
                            //         }else if(d.businessType=='repair'){
                            //             return '报修'
                            //         }else if(d.businessType=='recycle'){
                            //             return '报废'
                            //         }
                            //     }}
                            ,{field: 'creatorName', sort: true, title: '盘验人'}
                            ,{field: 'createTime', sort: true, title: '盘验时间'}
                            // ,{field: 'isCheck', sort: true, title: '是否盘验',templet:function (d) {
                            //         if(d.status=='checked'){
                            //             return '是';
                            //         }
                            //         return  '否';
                            //     }}

                            // ,{field: 'status', sort: true, title: '盘验状态',templet:function (d) {
                            //         if(d.status=='created'){
                            //             return '待盘验'
                            //         }
                            //         return  '已盘验'
                            //     }}
                            // ,{field: 'no', sort: true, title: 'no'}
                            // ,{field: 'day', sort: true, title: 'day'}
                            // ,{field: 'operationTypeDesc', sort: true, title: '操作类型'}
                            // ,{field: 'businessTypeDesc', sort: true, title: '业务类型'}
                            // ,{field: 'statusDesc', sort: true, title: '状态'}
                            // ,{field: 'checkTime', sort: true, title: '盘验时间'}
                            // ,{field: 'checkUserId', sort: true, title: '盘验人id'}
                            // ,{field: 'checkUserName', sort: true, title: '盘验人'}
                            // ,{field: 'notifyTime', sort: true, title: '通知时间'}
                            // ,{field: 'notifyUserId', sort: true, title: '通知人'}
                            // ,{field: 'notifyUserName', sort: true, title: '通知人'}
                            // ,{field: 'takeUserId', sort: true, title: '回收人id'}
                            // ,{field: 'takeUserName', sort: true, title: '回收人'}
                            // ,{field: 'takeTime', sort: true, title: '回收时间'}
                            // ,{field: 'inoutId', sort: true, title: '暂存柜存刀id'}
                            // ,{field: 'cabId', sort: true, title: '柜子id'}
                            // ,{field: 'skuId', sort: true, title: 'sku id'}
                            // ,{field: 'companyId', sort: true, title: '企业id'}
                            // ,{field: 'spaceId', sort: true, title: '货道id'}
                            // ,{field: 'operationTypeDesc', sort: true, title: '操作类型'}
                            // ,{field: 'businessTypeDesc', sort: true, title: '业务类型'}
                            // ,{field: 'quantityCurrent', sort: true, title: '当前库存'}
                            // ,{field: 'quantityAfter', sort: true, title: '变动后库存'}

                            ,{align: 'center', toolbar: '#storageInventoryInoutTableBar', title: '操作', width: 100}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(storageInventoryInoutTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') { //修改
                            showEditModel(data);
                        } else if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }else if(obj.event=='detail'){
                            // admin.req('enterprisecab/cabinet/admin/storageInventoryInout/detail/' + obj.data.id, {}, function (res) {
                            admin.req('enterprisecab/cabinet/admin/checkedResultDetail/detail/' + obj.data.id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    showEditModel(res.data);
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'GET');
                        }
                    });

                    form.on('submit(storageInventoryInoutBtnGenOrderSubmit2)', function (d) {
                        layer.load(2);
                        var detailList=[];
                        detailList.push({resultId:data.id,quantity:data.quantity})
                        d.field.detailList=detailList;
                        d.field.supplierName=$("#supplierId2 option:selected").data('name');
                        d.field.supplierCompanyId=$("#supplierId2 option:selected").data('companyid')
                        d.field.businessType='repair';
                        admin.req('enterprisecab/cabinet/admin/checkedResultOrder/save', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('storageCheckRepairResultTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');

                        return false;
                    });


                    function showEditModel(data) {
                        admin.open({
                            type: 1,
                            area: ['1280px', '800px'],
                            offset: '65px',
                            title: '详情',
                            content: $('#storageInventoryInoutForm').html(),
                            success: function () {
                                if (data) {
                                    var option = $("form select[name='operationType']").find("option");
                                    layui.each(option, function (index, item) {
                                        if (data.operationType == item.text) {
                                            data.operationType = item.value;
                                        }
                                    });
                                }

                                if (data) {
                                    var option = $("form select[name='businessType']").find("option");
                                    layui.each(option, function (index, item) {
                                        if (data.businessType == item.text) {
                                            data.businessType = item.value;
                                        }
                                    });
                                }

                                if (data) {
                                    var option = $("form select[name='status']").find("option");
                                    layui.each(option, function (index, item) {
                                        if (data.status == item.text) {
                                            data.status = item.value;
                                        }
                                    });
                                }

                                $(".model-form input").css("border", "none")
                                $(".model-form input").attr('readonly', true);

                                var insTb = table.render({
                                    elem: '#storageInventoryInoutDetailTable',
                                    // url: config.base + 'enterprisecab/cabinet/admin/storageInventoryInoutDetail/list?inoutId=' + data.id,
                                    url: config.base + 'enterprisecab/cabinet/admin/checkedResultLabel/list?resultDetailId=' + data.id,
                                    page: true,
                                    cellMinWidth: 100,
                                    cols: [[
                                        {type: 'numbers'}
                                        , {
                                            field: 'skuPic', width: 110 , sort: true, title: '物料图片', templet: function (d) {
                                                if (d.skuPic) {
                                                    return '<img src="'+ d.skuPic + '"/>';
                                                }
                                                return '<img src="assets/images/default-icon.png"/>';
                                            }
                                        }
                                        , {field: 'skuName', sort: true, title: '物料名称', width: 160}
                                        , {field: 'skuCode', sort: true, title: '物料编号', width: 160}
                                        , {field: 'skuSpecOld', sort: true, title: '规格', width: 160}
                                        , {field: 'productBrandName', sort: true, title: '品牌'}
                                        ,{field: 'cabName', sort: true, title: '柜子名称'}
                                        ,{field: 'cabCode', sort: true, title: '柜子编号', width: 130}
                                        , {field: 'spaceCode', sort: true, title: '所属货位'}
                                        , {field: 'quantity', sort: true, title: '数量'}
                                        , {field: 'code', sort: true, title: '盘验编号'}
                                        , {field: 'createTime', sort: true, title: '盘验时间'}
                                        ,{field: 'businessTypeDesc', sort: true, title: '盘验前状态', width: 120}
                                        ,{field: 'businessTypeCheckedDesc', sort: true, title: '盘验后状态', width: 120}

                                    ]],
                                    done: function (res, curr, count) {
                                        //移除按钮
                                        var menuButton = config.getMenuButton(location.hash);
                                        if (menuButton.indexOf("搜索") == -1) {
                                            $('.layui-form.toolbar').remove();
                                        }
                                        if (menuButton.indexOf("修改") == -1) {
                                            $("[lay-event='edit']").remove();
                                        }
                                        if (menuButton.indexOf("删除") == -1) {
                                            $("[lay-event='del']").remove();
                                        }
                                    }
                                });

                                form.val('storageInventoryInoutForm', data);
                                // 表单提交事件

                            }
                        });
                    }

                    //监听排序
                    table.on('sort(storageInventoryInoutTable)', function(obj){
                        table.reload('storageInventoryInoutTable', {
                            initSort: obj
                            ,where: {
                                sort: obj.field
                                ,order: obj.type
                            }
                        });
                    });

                    // 搜索
                    form.on('submit(formSubSearchLog)', function (data) {
                        insTb.reload({where: data.field,page: {curr: 1}},'data');
                    });
                }
            })
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '880px',
                offset: '65px',
                title: data ? '修改暂存柜存刀/取刀;存/取报修/回收刀' : '添加暂存柜存刀/取刀;存/取报修/回收刀',
                content: $('#storageInventoryInoutForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='operationType']").find("option");
                        layui.each(option, function(index, item){
                            if(data.operationType  == item.text){
                                data.operationType = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='businessType']").find("option");
                        layui.each(option, function(index, item){
                            if(data.businessType  == item.text){
                                data.businessType = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('storageInventoryInoutForm', data);
                    // 表单提交事件
                    form.on('submit(storageInventoryInoutFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprisecab/cabinet/admin/storageInventoryInout/update' : 'enterprisecab/cabinet/admin/storageInventoryInout/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('storageInventoryInoutTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/cabinet/admin/storageInventoryInout/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        /*$('#storageInventoryInoutBtnExport').click(function () {
            var checkRows = table.checkStatus('storageInventoryInoutTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });*/

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if(value){
                    let dateValue = value.split(" - ");
                    $("#startTime").val(dateValue[0]+" 00:00:00");
                    $("#endTime").val(dateValue[1]+" 23:59:59");
                }else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }
                setTimeout(function() {$('[lay-filter="formSubSearchLog"]').click()}, 300);
            }
        });

        // 导出
        $('#storageInventoryInoutBtnExport').click(function () {
            var sTime = $('#startTime').val();
            var eTime = $('#endTime').val();

            if (sTime == "" && eTime == ""){
                layer.msg('请先选择报修时间！')
            } else {

                location.href = config.base + 'enterprisecab/cabinet/admin/checkedRepairResult/inspectionAndRepairExport?startCreateTime='+sTime+'&endCreateTime='+eTime;
            }
        });

        // 渲染下拉框
        function loadReportSelectHtml(data, tips, htmltarget, type, parentName) {
            let html = ' <option value="">' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].creatorName}</option>`;
                }
            }
            $(parentName).find("select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>